// $codepro.audit.disable numericLiterals
/**
 * Security.java
 */
package edu.gatech.coc.betameisters.utilities;
import edu.gatech.coc.betameisters.people.Person;

/**
 * The Security class; takes care of password encryption 
 * @author Peter
 *
 * @version $Revision: 1.0 $
 */
public class Security {
	
	/** // $codepro.audit.disable unnecessaryDefaultConstructor
	 * Constructor that starts up the security class
	 */
	public Security(){ // $codepro.audit.disable emptyMethod

	}
	
	/**
	 * This will encrypt a password and store it in the person/database
	 * @param aPerson
	 * @param pass
	 */
	public void encryptPassword(Person aPerson, String pass){
		aPerson.setUserPass(encryption(pass));
	}
	
	/**
	 * This will check if the password from the input matches the password in the database
	 * @param aPerson
	 * @param inputPass
	 * @return true if it's a match, false otherwise
	 */
	public boolean checkPass(Person aPerson, String inputPass){ // $codepro.audit.disable booleanMethodNamingConvention
		final String encrpyted = aPerson.getUserPass();
		inputPass = encryption(inputPass); // $codepro.audit.disable questionableAssignment
		if(encrpyted.equals(inputPass)){ // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.codingStyle.returnBooleanExpressionValue
			return true;
		}
		else{
			return false;
		}
	}

	/**
	 * @param aPerson
	 * @return the encrypted password
	 */
	public String resetPassword(Person aPerson){
		final String temp = " ";
		aPerson.setUserPass(" ");
		return temp;
	}

	/**
	 * Encryption algorithm here
	 * @param aWord
	 * @return the encrypted word
	 */
	private String encryption(String aWord){
		char letter;
		int value;
		String encrypt = "";
		final char[] eWord = aWord.toCharArray();
		for(int i = aWord.length() - 1; i > 0; i--){
			value = (int) eWord[i];
			value = (i * 3 - 5) / 2; // $codepro.audit.disable multiplicationOrDivisionByPowersOf2
			if(value < 32){
				value += 32;
			}
			while(value > 126){
				value -= 10;
			}
			letter = (char) value; // $codepro.audit.disable lossOfPrecisionInCast
			encrypt += letter; // $codepro.audit.disable stringConcatenationInLoop
		}
		return encrypt;
	}
	
	/**
	 * Overrides toString method
	 * @return Security status
	 */
	public String toString(){
		return "Security";
	}
}
